﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using XiAnAirport.BUS;
using XiAnAirport.ElevatorWebSocket;
using XiAnAirport.Models;

namespace XiAnAirport.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HomePageController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        JwtToken _jwttoken = new JwtToken();
        IHttpContextAccessor _access;
        IWebSocketManage _webSocketManage;
        public HomePageController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access, IWebSocketManage webSocketManage
           )
        {
            _jwtSettings = jwtSettingsAccesser.Value;
            _webSocketManage = webSocketManage;
            _access = access;
        }

        /// <summary>
        /// 首页统计信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GetHomeData")]
        public ResponseMessage GetHomeData(AreaInfo areainfo)
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                ElevatorStateBUS mBUS = new ElevatorStateBUS();
                List<ElevatorState> elevatorStates = mBUS.GetData();
                message = BUS.GetHomeData(areainfo.id, areainfo.isAll, elevatorStates);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "首页统计信息获取失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询首页统计信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 请求首页各区域电梯数量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("GetAreaElevatorCount")]
        public ResponseMessage GetAreaElevatorCount()
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetAreaElevatorCount();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "首页各区域电梯数量获取失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询首页各区域电梯数量";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 请求首页电梯坐标
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("GetElevatorPositionXY")]
        public ResponseMessage GetElevatorPositionXY(AreaInfo areainfo)
        {
            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetElevatorPositionXY(areainfo.id, areainfo.isAll);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "首页电梯坐标获取失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询首页电梯坐标";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 请求首页电梯状态(通过缓存)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("GetElevatorState")]
        public ResponseMessage GetElevatorState(AreaInfo areainfo)
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                ElevatorStateBUS mBUS = new ElevatorStateBUS();
                List<ElevatorState> elevatorStates = mBUS.GetData();
                message = BUS.GetElevatorState(areainfo.id, areainfo.isAll, elevatorStates);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "首页电梯状态失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询首页电梯状态";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 首页未处理报修单数量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("GetFaultReportCount")]
        public ResponseMessage GetFaultReportCount(RepairWorkOrderInfo repairWorkOrder)
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetFaultCount(repairWorkOrder);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "未处理报修单数量获取失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询未处理报修单数量";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///获取大屏数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryHomePageData")]
        public ResponseMessage QueryHomePageData()
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetHomePageData();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "获取大屏数据";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///获取WebSocket地址
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryWebSocketUrl")]
        public ResponseMessage QueryWebSocketUrl()
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetWSUrl();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "获取WebSocket地址";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
